package flex.cc.nursing.api;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import flex.cc.PublicUtil;
import flex.cc.basicinformation.dao.ValueAddedServiceProviderDao;
import flex.cc.common.utils.DateUtil;
import flex.cc.core.support.BaseController;
import flex.cc.nursing.service.NursingOrderService;
import flex.cc.nursing.service.NursingSynStatusService;

/**
 * 
 * @author R
 * 2022年03月22日  14:04:52
 * @desc 护理类服务进度信息推送接口
 */
@RestController
@RequestMapping(value = "/storeapi/nursing")
public class NursingSynSatusController extends BaseController{
    
	@Resource
    private NursingSynStatusService nursingSynSatusService;
	@Resource
    private NursingOrderService nursingOrderService;
	@Resource
    private ValueAddedServiceProviderDao valueAddedserviceProviderDao;
	/**
	 * 
	 * @author R
	 * 2022年03月22日  14:04:52
	 * @desc 服务进度信息推送处理
	 */
	@ResponseBody
    @RequestMapping("/synStatus")
    public JSONObject synSatus(HttpServletRequest request) throws Exception{
		JSONObject jsonResult = new JSONObject();
		long startTime = System.currentTimeMillis();
    	logger.info("NursingSynSatusController 服务进度信息推送接口:begintime="+DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
    	//状态同步业务处理
    	Boolean flag = true;
        String responseMsg = "成功";
        
        String reserveNo = "";
        //读取报文
        try {
            // 设置编码
            request.setCharacterEncoding("UTF-8");
            // 读取请求报文
            String requestJson = IOUtils.toString(request.getInputStream(), "UTF-8");
            if (requestJson != null && !"".equals(requestJson)) {
            	logger.info("NursingSynSatusController 服务进度信息推送接口接收报文： "+requestJson);
                // 调用业务处理 =============================
            	logger.info("nursingApi/synSatus requestJson:" + requestJson);
                JSONObject synSatusRequestBody = JSONObject.parseObject(requestJson);
                Map<String, Object> synSatusRequestBodyMap = synSatusRequestBody.toJavaObject(Map.class);
                
                //获取请求报文中的参数 如果为空直接返回
                JSONObject busBodyObj = (JSONObject) synSatusRequestBodyMap.get("bus_body");
                if(PublicUtil.isNotEmpty(busBodyObj)){
                    Map<String, Object> busBodyMap = busBodyObj.toJavaObject(Map.class);
                    //获取预约编号
                	reserveNo = (String) busBodyMap.get("reserveNo");
                	if(PublicUtil.isNotEmpty(reserveNo)){
                		flag = true;
                		//服务进度推送接口处理
                        jsonResult = nursingSynSatusService.nursingSynStatus(synSatusRequestBodyMap); 
                        logger.info("nursingApi/synSatus： "+jsonResult);
                	}else{
                		flag = false;
                        responseMsg = "接口处理异常:请求报文无法获取预约单号";
                        logger.error("nursingApi/synSatus 接口处理异常:请求报文无法获取预约单号");
                	}
                }
            } else {
                flag = false;
                responseMsg = "接口处理异常:请求报文为空";
                logger.error("nursingApi/synSatus 接口处理异常:请求报文为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            responseMsg = "数据接收异常";
            logger.error("nursingApi/synSatus 数据接收异常:" + e.getMessage());
        }
        if(!flag){
        	// 生成响应报文
        	jsonResult.put("reserveNo", reserveNo);
    		jsonResult.put("bussResCode", "99");
    		jsonResult.put("bussResMsg", "该订单业务处理错误，错误信息为："+responseMsg);
        }
    	long endTime = System.currentTimeMillis();
    	logger.info("NursingSynSatusController 服务进度信息推送接口:endtime="+DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")+"; 处理时长="+(endTime - startTime)+"; resJson=?"+jsonResult.toString());
		return jsonResult;
	}
	
	
	/**
	 * 
	 * @author R
	 * 2022年03月22日  14:04:52
	 * @desc 预约
	 */
	@ResponseBody
    @RequestMapping("/createAppointment")
    public JSONObject createAppointment(HttpServletRequest request) throws Exception{
		JSONObject jsonResult = new JSONObject();
		long startTime = System.currentTimeMillis();
    	logger.info("NursingSynSatusController 预约接口:begintime="+DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
    	//状态同步业务处理
    	Boolean flag = true;
        String responseMsg = "成功";
        
        String reserveNo = "";
        //读取报文
        try {
            // 设置编码
            request.setCharacterEncoding("UTF-8");
            // 读取请求报文
            String requestJson = IOUtils.toString(request.getInputStream(), "UTF-8");
            if (requestJson != null && !"".equals(requestJson)) {
            	logger.info("NursingSynSatusController 预约接口接收报文： "+requestJson);
                // 调用业务处理 =============================
            	logger.info("nursingApi/synSatus requestJson:" + requestJson);
                JSONObject synSatusRequestBody = JSONObject.parseObject(requestJson);
                Map<String, Object> synSatusRequestBodyMap = synSatusRequestBody.toJavaObject(Map.class);
                reserveNo = (String) synSatusRequestBodyMap.get("reserveNo");
                flag = true;
                jsonResult = nursingOrderService.createAppointment(synSatusRequestBodyMap); 
                logger.info("nursingApi/createAppointment： "+jsonResult);
            } else {
                flag = false;
                responseMsg = "接口处理异常:请求报文为空";
                logger.error("nursingApi/createAppointment 接口处理异常:请求报文为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
            responseMsg = "数据接收异常";
            logger.error("nursingApi/createAppointment 数据接收异常:" + e.getMessage());
        }
        if(!flag){
        	// 生成响应报文
        	jsonResult.put("reserveNo", reserveNo);
    		jsonResult.put("bussResCode", "99");
    		jsonResult.put("bussResMsg", "该订单业务处理错误，错误信息为："+responseMsg);
        }
    	long endTime = System.currentTimeMillis();
    	logger.info("NursingSynSatusController 预约接口:endtime="+DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")+"; 处理时长="+(endTime - startTime)+"; resJson=?"+jsonResult.toString());
		return jsonResult;
	}
}
